<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class SlideImagesController extends AppController {

    var $name = 'SlideImages';
    var $components = array(
        'Session', 'Alias', 'Upload'
    );
    var $helpers = array(
        'Editor', 'Published'
    );
    var $uses = array('Slideimages');

    function afterFilter() {
        //update cache
        $this->Slideimages->getImages();
    }

    function index() {

        $this->set('title_for_layout', Configure::read('site_name') . ' - thư viện ảnh');
        $page = array_key_exists('page', $this->params['named']) ? $this->params['named']['page'] : 1;
        $basket_id = array_key_exists('basket_id', $this->params['named']) ? $this->params['named']['basket_id'] : 0;

        $search = !empty($this->params['form']['search']) ? $this->params['form']['search'] : false;
        $conditions = array();

        //search

        if ($search) {
            $conditions['OR'] = array(
                'title LIKE' => "%$search%",
            );
        }
        $this->paginate = array(
            'conditions' => $conditions,
            'limit' => Configure::read('limit_list'),
            'order' => array(
                'Slideimages.ordering' => 'DESC'
            ),
        );
        $data = $this->paginate('Slideimages');

        $this->set('total', $this->params['paging']['Slideimages']['count']);
        $this->set('data', $data);
        $this->set('limit_page', $this->paginate['limit']);
        $this->set('start_page', $page);
    }

    function remove() {
        $conditions = array(
            "{$this->Slideimages->alias}.{$this->Slideimages->primaryKey} IN (" . implode(',', $this->params['form']['cid']) . ')'
        );
        //delete images small
        $data = $this->Slideimages->find('list', array(
                    'conditions' => $conditions,
                    'fields' => array('Slideimages.id', 'Slideimages.images')
                        )
        );
        foreach ($data AS $image_file) {
            //delete it
            if (is_file(Configure::read('slide_img_absolute_path') . $image_file) && $image_file != 'no_image.gif') {
                $this->Upload->delete(Configure::read('slide_img_absolute_path') . $image_file);
            }
        }

        if ($this->Slideimages->deleteAll($conditions)) {
            $this->Session->setFlash(sprintf("Xóa thành công %d hình ảnh", sizeof($this->params['form']['cid'])), 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash('Không thể xóa', 'msg', array(), 'error');
        }
        $this->redirect(array('action' => 'index'));
    }

    function edit() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - Chỉnh sữa thông tin');
        $params = array_merge($this->params['form'], $this->params['named']);
        if (isset($params['cid']) && is_array($params['cid'])) {
            $this->Slideimages->id = $params['cid'][0];
        } else {
            $this->Slideimages->id = array_key_exists('id', $params) ? $params['id'] : 0;
        }
        if (empty($this->data)) {
            $fields = array(
                'title', 'images', 'id'
            );
            $this->data = $this->Slideimages->read($fields);
            $check_thumbnail_exist = is_file(Configure::read('slide_img_absolute_path') . $this->data['Slideimages']['images']);
            $this->set('check_thumbnail_exist', $check_thumbnail_exist);
            $this->set('image_url', Configure::read('slide_img_url_path') . $this->data['Slideimages']['images']);
        }
    }

    function add() {
        $this->set('title_for_layout', Configure::read('site_name') . ' - thêm  thư viện ảnh');
    }

    function save() {
        $data = $this->data['Slideimages'];

        //for upload thumbnail
        $this->Upload->setUploadpath(Configure::read('slide_img_absolute_path'));
        $this->Upload->setRules('size', Configure::read('content_image_maxsize'));
        $this->Upload->setRules('ext', Configure::read('content_image_ext'));
        $row = $this->Slideimages->findById($this->data['Slideimages']['id']);

        //upload small thumbnail
        if ($this->data['Slideimages']['images']['name'] != '') {
            if ($this->Upload->validate($this->data['Slideimages']['images'])) {
                $data['images'] = $this->Upload->upload($this->data['Slideimages']['images']);

                //delete it
                if (is_file(Configure::read('slide_img_absolute_path') . $row['Slideimages']['images']) && $row['Slideimages']['images'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('slide_img_absolute_path') . $row['Slideimages']['images']);
                }
            } else {
                $this->Session->setFlash($this->Upload->getError(), 'error', array(), 'msg');
                $this->redirect(array('controller' => 'slide_images', 'action' => 'index'));
                return;
            }
        } else {
            unset($data['images']);
            //delete image
            if (!empty($this->data['Slideimages']['delete_image'])) {

                if (is_file(Configure::read('slide_img_absolute_path') . $row['Slideimages']['images']) && $row['Slideimages']['images'] != 'no_image.gif') {
                    $this->Upload->delete(Configure::read('slide_img_absolute_path') . $row['Slideimages']['images']);
                    $data['images'] = 'no_image.gif';
                }
            }
        }
        if(empty($data['id'])){
            $data['ordering']   =   $this->Slideimages->nextOrder();
        }
        //end upload thumbnail
        if ($this->Slideimages->save($data)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Slideimages->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'slide_images', 'action' => 'index'));
    }

    function cancel() {
        $this->redirect(array('controller' => 'slide_images', 'action' => 'index'));
    }

    function published() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 1,
            );
        }

        if ($this->Slideimages->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Slideimages->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'slide_images', 'action' => 'index'));
    }

    function unpublished() {
        $cid = $this->params['form']['cid'];
        $conditions = array();
        foreach ($cid AS $id) {
            $conditions[] = array(
                'id' => $id,
                'published' => 0,
            );
        }

        if ($this->Slideimages->saveAll($conditions)) {
            $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');
        } else {
            $this->Session->setFlash($this->Slideimages->invalidFields(), 'error', array(), 'msg');
        }
        $this->redirect(array('controller' => 'slide_images', 'action' => 'index'));
    }
    function saveorder() {
        $ordering = $this->params['form']['order'];

        foreach ($ordering AS $id => $order) {
            $this->Slideimages->set('id', $id);
            $this->Slideimages->saveField('ordering', $order);
        }
        $this->Session->setFlash('lưu thành công thông tin', 'msg', array(), 'msg');

        $this->redirect(array('action' => 'index'));
    }
}

?>
